package org.oexam.ostudy.sl;

import java.util.List;

import org.oexam.ostudy.db.UsersAccess;
import org.oexam.ostudy.facade.struct.StudentDetails;
import org.oexam.ostudy.facade.struct.StudentList;
import org.oexam.ostudy.struct.Students;
import org.oexam.ostudy.struct.Teachers;
import org.oexam.ostudy.struct.Users;

public class ManageStudentList {
	
	
	public StudentList getStudentList(){
		
        StudentList list = new StudentList();
		
        UsersAccess details = new UsersAccess();
		
		List<Students> studentList = details.getStudents();
		list.setStudentList(studentList);
		
		return list;
	}
	
	public StudentDetails getStudentDetails(int student_id) {
		
		UsersAccess details = new UsersAccess();
		Students student = details.getStudent(student_id);
		if (student == null) {
			return null;
		}
		
		int user_id = student.getUser_id();
		Users user = details.getUser(user_id);
		if (user == null) {
			return null;
		}
		
		StudentDetails sd = new StudentDetails();
		sd.setStudent_id(student.getStudent_id());
		sd.setUserName(user.getUser_name());
		sd.setPassword(user.getPassword());
		sd.setFirstName(student.getFirst_name());
		sd.setLastName(student.getLast_name());
		sd.setAddress(student.getAddress());
		sd.setUser_id(student.getUser_id());
		sd.setPhone(student.getPhone());
		sd.setEmail(student.getEmail());
		// get details of teacher 
		Teachers teacher = details.getTeacher(student.getTeacher_responsible());
		String teacherName = teacher.getFirst_name() + " " + teacher.getLast_name();
		sd.setTeacher_responsible(teacherName);
		
		
		return sd;
    } 
	
	public boolean deleteStudent(int student_id) {
		UsersAccess details = new UsersAccess();
		Students student = details.getStudent(student_id);
		if (student == null) {
			return false;
		}
		
		int user_id = student.getUser_id();
		Users user = details.getUser(user_id);
		if (user == null) {
			return false;
		}
		
		details.deleteStudent(student, user);
		
		return true;
	}	
	
	
}
